package cn.tedu.ivos.user.controller;


import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/v1/user/")
@Api(tags = "用户模块")
public class UserController {
    @Autowired(required = false)
    private UserService userService;



    @PostMapping("login")
    @ApiOperation(value = "登录")
    public JsonResult login( UserLoginParam userLoginParam){
        log.debug("用户登录：userLoginParam={}",userLoginParam);
        UserVO userVO=userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }


    @PostMapping("save")
    @ApiOperation(value = "添加用户")
    public JsonResult saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户：userSaveParam={}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("getall")
    @ApiOperation(value = "获取所有用户")
    public JsonResult getAllUser(){
        List<User> allUser = userService.getAllUser();
        allUser.stream().map((user)->{
            user.setPassword(null);
            return user;
        }).collect(Collectors.toList());
        return JsonResult.ok(allUser);
    }




    @GetMapping("select")
    @ApiOperation(value = "获取用户")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug(" 查询用户：userQuery={}",userQuery);
        List<UserVO> list=userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }


    @PostMapping("update/password/{userId}")
    @ApiOperation(value = "重置密码")
    public JsonResult resetPwd(@PathVariable Long userId){
        log.debug("重置密码:userId={}",userId);
        userService.resetPwd(userId);
        return JsonResult.ok();
    }


    @PostMapping("update/status/{userId}/{status}")
    @ApiOperation(value = "更新状态")
    public JsonResult updateStatus(@PathVariable Long userId,String status){
        log.debug("修改用户状态:userId={},status={}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @PostMapping("deleteUser/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId){
        log.debug("删除用户:userId={}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }


}
